package direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import util.Tools;

/**
 * 描述：direct类型的交换机，根据routingKey来路由消息
 */

public class EmitLogDirect {
    private static final String EXCHANGE_NAME = "direct_logs";
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = Tools.getFactory();
        Connection connection = factory.newConnection();
        connection.createChannel();
        // 声明一个direct类型的交换机
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 发送INFO级别的消息
        String message = "Hello World!";
        channel.basicPublish(EXCHANGE_NAME, "info", null, message.getBytes("UTF-8"));
        System.out.println("发送了消息，等级为info，内容为：" + message);

        // 发送WARNING级别的消息
        channel.basicPublish(EXCHANGE_NAME, "warning", null, message.getBytes("UTF-8"));
        System.out.println("发送了消息，等级为warning，内容为：" + message);

        // 发送ERROR级别的消息
        channel.basicPublish(EXCHANGE_NAME, "error", null, message.getBytes("UTF-8"));
        System.out.println("发送了消息，等级为error，内容为：" + message);

        // 关闭连接
        channel.close();
        connection.close();
    }
}
